using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class MQuyenDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the mQuyen table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, MQuyenDTO mQuyen)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mQuyen", mQuyen);

			object[] parameters = new object[]
			{
				Null.GetNull(mQuyen.Qu_parent, DBNull.Value),
				Null.GetNull(mQuyen.Qu_ten, ""),
				Null.GetNull(mQuyen.Qu_code, "")
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mQuyenInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mQuyenInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mQuyenInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the mQuyen table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, MQuyenDTO mQuyen)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mQuyen", mQuyen);

			object[] parameters = new object[]
			{
				Null.GetNull(mQuyen.QU_ID, DBNull.Value),
				Null.GetNull(mQuyen.Qu_parent, DBNull.Value),
				Null.GetNull(mQuyen.Qu_ten, ""),
				Null.GetNull(mQuyen.Qu_code, "")
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mQuyenUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mQuyenUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mQuyenUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mQuyen table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int qU_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(qU_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mQuyenDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mQuyenDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mQuyenDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mQuyen table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mQuyenDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mQuyenDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mQuyenDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the mQuyen table.
		/// </summary>
		public static MQuyenDTO Select(Database objDb, int qU_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(qU_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mQuyenSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the mQuyen table.
		/// </summary>
		public static DataTable Select(Database objDb, int qU_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(qU_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mQuyenSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the mQuyen table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("mQuyenSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the mQuyen table.
		/// </summary>
		public static List<MQuyenDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mQuyenSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mQuyen table.
		/// </summary>
		public static List<MQuyenDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mQuyenSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mQuyen table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mQuyenSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mQuyen table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mQuyenSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the MQuyenDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static MQuyenDTO MapDataReader(IDataReader dataReader)
		{
			MQuyenDTO mQuyenDTO = new MQuyenDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return mQuyenDTO;
			mQuyenDTO.QU_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["QU_ID"]);
			mQuyenDTO.Qu_parent = clsCommon.fncCnvNullToInt(dt.Rows[0]["qu_parent"]);
			mQuyenDTO.Qu_ten = clsCommon.fncCnvNullToString(dt.Rows[0]["qu_ten"]);
			mQuyenDTO.Qu_code = clsCommon.fncCnvNullToString(dt.Rows[0]["qu_code"]);

			return mQuyenDTO;
		}
		/// <summary>
		/// Creates a new instance of the MQuyenDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<MQuyenDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<MQuyenDTO> lstResult = new List<MQuyenDTO>();
			MQuyenDTO mQuyenDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			mQuyenDTO = new MQuyenDTO();
			mQuyenDTO.QU_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["QU_ID"]);
			mQuyenDTO.Qu_parent = clsCommon.fncCnvNullToInt(dt.Rows[i]["qu_parent"]);
			mQuyenDTO.Qu_ten = clsCommon.fncCnvNullToString(dt.Rows[i]["qu_ten"]);
			mQuyenDTO.Qu_code = clsCommon.fncCnvNullToString(dt.Rows[i]["qu_code"]);
lstResult.Add(mQuyenDTO);
}

			return lstResult;
		}

		#endregion
	}
}
